﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Headquarters : MonoBehaviour
{
    public GameObject Target;
    public string Influence;
    // Start is called before the first frame update
    void Start()
    {
        if(Influence != "")
            for(int i=0; i<transform.childCount; ++i)
            {
                var childObj = transform.GetChild(i);
                Ship childShip = childObj.GetComponent<Ship>();
                if(childShip)
                    childShip.m_ShipStruct.Influence = Influence;
            }
    }

    // Update is called once per frame
    void Update()
    {

        int shipCount = 0;
        for(int i=0; i<transform.childCount; ++i)
        {
            var child = transform.GetChild(i);
            var driver = child.GetComponent<Driver>();
            Ship ship = child.GetComponent<Ship>();

            if (ship != null)
                shipCount++;
            if (!driver)
                continue;
            if (Target)
                if (driver.m_CurrentState == Driver.State.STATE_STANDBY)
                {
                    driver.m_CurrentState = Driver.State.STATE_MOVE;
                    driver.Target = Target;

                }
        }
        if (shipCount == 0)
        {
            Destroy(gameObject);
        }
    }


    /// <summary>
    /// 请求支援，战斗力差距评估应该是敌方范围内的战斗力总和与我方范围内总和的差
    /// </summary>
    /// <param name="target"></param>
    /// <param name="me"></param>
    /// <param name="supportFightingCapacity"></param>
    public void SupportMe(GameObject target, GameObject me, float supportFightingCapacity)
    {
        List<ArmTarget> supportList = new List<ArmTarget>();

        for(int i=0; i<transform.childCount; ++i)
        {
            var supportShipObj = transform.GetChild(i);

            if(supportShipObj.GetComponent<ShipFactory>())
                continue;
            
            //  询问舰船是否已经有目标
            var supportDriver = supportShipObj.GetComponent<Driver>();
            if (supportDriver == null || supportDriver.Target != null)
                continue;

            //  是否离被支援舰船近
            float supportShipDistance = (supportShipObj.transform.position - me.transform.position).magnitude;
            if (supportShipDistance > 20)
                return;
            supportList.Add(new ArmTarget(supportShipObj.gameObject, supportShipDistance));
        }


        //  先用距离排个序
        ArmTarget tempTarget = null;
        for(int i=0; i<supportList.Count; ++i)
        {
            for(int j=i+1; j<supportList.Count; ++j)
            {
                if(supportList[i].Distance > supportList[j].Distance)
                {
                    tempTarget = supportList[i];
                    supportList[i] = supportList[j];
                    supportList[j] = tempTarget;
                }
            }
        }

        //  从距离最近的舰船中挑选战舰去支援
        foreach(var supportShipI in supportList)
        {
            var supportShipObj = supportShipI.Target;
            if (!supportShipObj)
                continue;

            //  计算舰船战斗力，进行支援
            var supportShip = supportShipObj.GetComponent<Ship>();
            var supportDriver = supportShipObj.GetComponent<Driver>();
            if (supportShip && supportDriver)
            {
                supportFightingCapacity -= supportShip.FightingCapacity;
                supportDriver.Target = target;
            }

            //  支援已经足够
            if (supportFightingCapacity <= 0)
            {
                break;
            }
        }
    }

    public void FoundTarget(GameObject target)
    {
        List<ArmTarget> supportList = new List<ArmTarget>();

        for (int i = 0; i < transform.childCount; ++i)
        {
            var supportShipObj = transform.GetChild(i);

            if (supportShipObj.GetComponent<ShipFactory>())
                continue;

            //  询问舰船是否已经有目标
            var supportDriver = supportShipObj.GetComponent<Driver>();
            var supportShip = supportShipObj.GetComponent<Ship>();
            if (supportDriver == null || supportDriver.Target != null)
                continue;

            //  是否离被支援舰船近
            float supportShipDistance = (supportShipObj.transform.position - target.transform.position).magnitude;
            if (supportShipDistance > supportShip.m_ShipStruct.DetectRange * 3)
                return;
            supportList.Add(new ArmTarget(supportShipObj.gameObject, supportShipDistance));
            supportDriver.Target = target;
        }
    }

    /// <summary>
    /// 找到瞄准到目标的船
    /// </summary>
    /// <returns></returns>
    public List<GameObject> GetShipsOfArmTarget(GameObject target)
    {
        List<GameObject> retValues = new List<GameObject>();
        for(int i=0; i<transform.childCount; ++i)
        {
            var childObj = transform.GetChild(i).gameObject;
            if (childObj.GetComponent<ShipFactory>())
                continue;

            var childDriver = childObj.GetComponent<Driver>();
            if(childDriver)
            {
                if(target == childDriver.Target)
                {
                    retValues.Add(childObj);
                }
            }
        }
        return retValues;
    }
}
